//
//  BaseAnchorViewController.swift
//  DouYuZB
//
//  Created by wangpeng on 2017/2/25.
//  Copyright © 2017年 鼎商动力. All rights reserved.
//

import UIKit
private let kItemMargin: CGFloat = 10


private let kHeaderViewH: CGFloat = 50
private let kNormalCellID = "kNormalCellID"

private let kHeaderViewID = "kHeaderViewID"
let kPrettyCellID = "kPrettyCellID"
let kNormalItemW = (kScreenW - kItemMargin * 3) / 2
let kNormalItemH = kNormalItemW * 3 / 4
let kPrettyItemH = kNormalItemW * 4 / 3
class BaseAnchorViewController: BaseViewController {
    
    //MARK: - 属性设置
    var baseVM: BaseViewModel!
    
    lazy var collectionView: UICollectionView = { [unowned self] in
        //1.创建布局
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kNormalItemW, height: kNormalItemH)
        layout.minimumLineSpacing  = 0
        layout.minimumInteritemSpacing = kItemMargin
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        layout.headerReferenceSize  = CGSize(width: kScreenW, height: kHeaderViewH)
        
        //2.创建UICollectionView
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
        
        collectionView.register(UINib(nibName: "CollectionNormalCell", bundle: nil), forCellWithReuseIdentifier: kNormalCellID)
        
        collectionView.register(UINib(nibName: "CollectionPrettyViewCell", bundle: nil), forCellWithReuseIdentifier: kPrettyCellID)
        
        collectionView.register( UINib(nibName: "CollectionReusableView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeaderViewID)
        return collectionView
        
        }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        loadData()
    }
    
}
//MARK: - 设置UI界面
extension BaseAnchorViewController{
    override func setupUI()  {
        //1.给父类的View赋值
        contentsView = collectionView
        //2.添加collectionView
        view.addSubview(collectionView)
        //3.调用super.setupUI
        super.setupUI()
    }
}

//MARK: - 请求数据
extension BaseAnchorViewController{
    func loadData() {
        
    }
}

//MARK: - 遵守UICollectionView数据源
extension BaseAnchorViewController:UICollectionViewDataSource{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return baseVM.anchorGroup.count
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return baseVM.anchorGroup[section].anchors.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //1.取出cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kNormalCellID, for: indexPath) as! CollectionNormalCell
        
        //2.给cell 赋值
        cell.anchor = baseVM.anchorGroup[indexPath.section].anchors[indexPath.item]
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeaderViewID, for: indexPath) as! CollectionReusableView
        headView.group = baseVM.anchorGroup[indexPath.section]
        return headView
    }
    
}
//MARK: - 遵守UICollectionView代理协议
extension BaseAnchorViewController:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("点击了\(indexPath.item)行")
        
        let anchor =  baseVM.anchorGroup[indexPath.section].anchors[indexPath.item]
        anchor.isVertical == 0 ?pushNormalRoomVc() : presentShowRoomVc()
    }
    
    private func presentShowRoomVc (){
        //1.创建 showRoomVc
        let showRoomVc = RoomShowViewController()
        //2.以模态方式弹出
        present(showRoomVc, animated: true, completion: nil)
    }
    private func pushNormalRoomVc (){
        //1.创建 NormalVc
        let normalVc = RoomNormalViewController()
        //2.以push 方式弹出
        navigationController?.pushViewController(normalVc, animated: true)
    }
    
}
